package matlap.client.view.guest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import matlap.client.services.article.ArticleService;
import matlap.client.services.article.ArticleServiceAsync;
import matlap.shared.Article;
import matlap.shared.Author;
import matlap.shared.Topic;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

/**
 * By clicking to Cikkek menu at the web page, appears the ArticleComposite,
 * where we can see the articles from the database.
 * 
 */

public class ArticlesComposite extends Composite {
	ArticleServiceAsync c1 = GWT.create(ArticleService.class);
	ArticleServiceAsync c2 = GWT.create(ArticleService.class);

	Panel bigPanel;
	VerticalPanel verticalPanel;
	Label lblNewLabel;
	ListBox listBox;
	HorizontalPanel horizontalPanel_1;
	Label lblNewLabel_1;
	Label lblNewLabel_2;
	Label lblNewLabel_3;
	Label lblNewLabel_4;
	Label lblNewLabel_5;
	Label lblNewLabel_6;
	Label lblNewLabel_7;
	HorizontalPanel horizontalPanel;
	TextBox textBox;
	ListBox authorsComboBox;
	ListBox topicsComboBox;
	Button button;
	String articleName = new String("");
	String topicName = new String("");
	String authorName = new String("");
	List<Article> articleList;
	VerticalPanel gridPanel = new VerticalPanel();
	Grid g;
	private Label lblNewLabel_8;
	private Label lblNewLabel_9;
	private Label lblNewLabel_10;
	private Label lblNewLabel_11;

	public ArticlesComposite(Panel bp) {
		
		bigPanel = bp;
		init();

		c1.selectAuthorName(new AsyncCallback<List<Author>>() {

			public void onSuccess(List<Author> result) {
				Iterator<Author> it = result.iterator();
				while (it.hasNext()) {
					Author aut = (Author) it.next();
					authorsComboBox.addItem(aut.getAuthorName());
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Sikertelen");
			}

		});

		c1.selectTopicsName(new AsyncCallback<List<Topic>>() {

			public void onSuccess(List<Topic> result) {
				Iterator<Topic> it = result.iterator();
				while (it.hasNext()) {
					Topic top = (Topic) it.next();
					topicsComboBox.addItem(top.getTopicsName());
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Sikertelen");
			}
		});

		button.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				int i = authorsComboBox.getSelectedIndex();
				authorName = authorsComboBox.getItemText(i);

				i = topicsComboBox.getSelectedIndex();
				topicName = topicsComboBox.getItemText(i);

				articleName = textBox.getText();

				c1.selectArticle(articleName, topicName, authorName,
						new AsyncCallback<List<Article>>() {

							public void onSuccess(List<Article> result) {

								initGridPanel(articleName, topicName, authorName);

							}

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Sikertelen");
							}
						});
			}

		});

		
	}

	public void init() {
		verticalPanel = new VerticalPanel();
		bigPanel.add(verticalPanel);
		verticalPanel.setSize("647px", "375px");

		lblNewLabel = new Label(
				"A k\u00F6vetkez\u0151 cikkek megtekinthet\u0151ek:");
		verticalPanel.add(lblNewLabel);
		verticalPanel.setCellHorizontalAlignment(lblNewLabel,
				HasHorizontalAlignment.ALIGN_CENTER);
		lblNewLabel.setSize("600px", "39px");
		lblNewLabel.setStyleName("BigBold");

		listBox = new ListBox();

		horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel_1.setSize("600px", "29px");
		verticalPanel.add(horizontalPanel_1);

		lblNewLabel_1 = new Label("C\u00EDm");
		horizontalPanel_1.add(lblNewLabel_1);
		horizontalPanel_1.setCellHorizontalAlignment(lblNewLabel_1,
				HasHorizontalAlignment.ALIGN_CENTER);

		lblNewLabel_4 = new Label("");
		horizontalPanel_1.add(lblNewLabel_4);

		lblNewLabel_3 = new Label("");
		horizontalPanel_1.add(lblNewLabel_3);
		horizontalPanel_1.setCellHorizontalAlignment(lblNewLabel_3,
				HasHorizontalAlignment.ALIGN_RIGHT);

		lblNewLabel_7 = new Label("  ");
		horizontalPanel_1.add(lblNewLabel_7);

		lblNewLabel_5 = new Label("            Szerz\u0151");
		horizontalPanel_1.add(lblNewLabel_5);
		horizontalPanel_1.setCellHorizontalAlignment(lblNewLabel_5,
				HasHorizontalAlignment.ALIGN_RIGHT);

		lblNewLabel_6 = new Label("");
		horizontalPanel_1.add(lblNewLabel_6);

		lblNewLabel_2 = new Label("T\u00E9mak\u00F6r");
		horizontalPanel_1.add(lblNewLabel_2);
		horizontalPanel_1.setCellHorizontalAlignment(lblNewLabel_2,
				HasHorizontalAlignment.ALIGN_CENTER);

		horizontalPanel = new HorizontalPanel();
		horizontalPanel.setSize("600px", "28px");
		verticalPanel.add(horizontalPanel);

		textBox = new TextBox();
		horizontalPanel.add(textBox);

		authorsComboBox = new ListBox();
		authorsComboBox.addItem("");
		horizontalPanel.add(authorsComboBox);
		authorsComboBox.setSize("29", "36");

		topicsComboBox = new ListBox();
		topicsComboBox.addItem("");
		horizontalPanel.add(topicsComboBox);
		topicsComboBox.setSize("29", "34");

		button = new Button("Keres");
		horizontalPanel.add(button);

		verticalPanel.add(gridPanel);
		initGridPanel("","","");
	}
	
	public void initGridPanel(String articleName, String topicName, String authorName){
		gridPanel.clear();
		g = new Grid();
		g.resize(1, 4);
		g.setText(0, 0, "megtekintes");
		g.setText(0, 1, "szerzo");
		g.setText(0,2,"cim");
		g.setText(0,3,"tema");
		g.setBorderWidth(2);
		gridPanel.add(g);
		g.setWidth("610px");
		
		lblNewLabel_8 = new Label("Megtekint\u00E9s");
		lblNewLabel_8.setStyleName("Header");
		g.setWidget(0, 0, lblNewLabel_8);
		
		lblNewLabel_9 = new Label("Szerz\u0151");
		lblNewLabel_9.setStyleName("Header");
		g.setWidget(0, 1, lblNewLabel_9);
		
		lblNewLabel_10 = new Label("C\u00EDm");
		lblNewLabel_10.setStyleName("Header");
		g.setWidget(0, 2, lblNewLabel_10);
		
		lblNewLabel_11 = new Label("T\u00E9ma");
		lblNewLabel_11.setStyleName("Header");
		g.setWidget(0, 3, lblNewLabel_11);
		
		c1.getArticlesNumber(new AsyncCallback<Integer>(){

			public void onFailure(Throwable caught) {
				Window.alert("Gond");
				
			}

			public void onSuccess(Integer result) {
				g.resize(result+1,4);
				
			}
			
		});
		
		c1.selectArticle(articleName, topicName, authorName, new AsyncCallback<List<Article>>(){

			public void onFailure(Throwable caught) {
				Window.alert("Gond");
				
			}

			@SuppressWarnings("deprecation")
			public void onSuccess(List<Article> result) {
				Iterator<Article> it = result.iterator();
				articleList = new ArrayList<Article>();
				int i=1;
				while (it.hasNext()){
					Article art = it.next();
					articleList.add(art);
					final Hyperlink numberlink = new Hyperlink(i+"", false, "newHistoryToken");
					numberlink.addClickHandler(new ClickHandler(){

						public void onClick(ClickEvent event) {
							int index = Integer.parseInt(numberlink.getText());
							bigPanel.clear();
							Frame fr = new Frame();
							fr.setSize("140%", "200%");
							fr.setUrl(articleList.get(index-1).getArticle_text());
							bigPanel.add(fr);
						}
						
						
					});
					g.setWidget(i, 0, numberlink);
					g.setText(i, 1, art.getAuthor());
					g.setText(i, 2, art.getTitle());
					g.setText(i, 3, art.getTopic());
					i++;
				}
				g.resize(i, 4);
			}
			
		});
	}
}